package Demo;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

//改
public class Demo4 {
    public static void main(String[] args) throws SQLException {
        Scanner sc = new Scanner(System.in);
        // 1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");

        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("12s34567");

        // 2.和数据库服务器建立网络连接
        Connection connection = dataSource.getConnection(); //封装了拨号和接通的操作

        // 3.写sql发给sql服务器
        System.out.print("请输入新的名称：");
        String name=sc.next();
        String sql = "update student set name=? where id=3";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        // 4.将语句发送给服务器执行，n为当前操作影响了多少行
        int n = statement.executeUpdate();
        //    statement.executeQuery();
        System.out.println("n=" + n);
        // 5.释放相关网络资源
        statement.close();
        connection.close();
    }
}
